import { Module } from 'vuex'
import { getItem } from '@/utils/storage'
// initial state
const stateData = {
  user: {},
  form: {
    cookie: false,
    username: '',
    password: ''
  },
  token: '',
  roles: false // 路由是否处理判断
}

// 使用typeof推导ts类型
export type State = typeof stateData

const state = getItem<State>('user') || stateData

const store:Module<State, any> = {
  namespaced: true, // 为模块开启命名空间
  state,
  getters: {

  },
  mutations: {
    setUser (state, user) {
      state.user = user || {}
    },
    setForm (state, form) {
      state.form = form
    },
    setToken (state, obj) {
      state.token = obj
    },
    setRoles: (state, roles) => {
      state.roles = roles
    }
  },
  actions: {
    logOut ({
      commit
    }) {
      commit('setUser')
      commit('setToken')
      commit('setRoles', false)
    }
  }
}
export default store
